'use strict';
import SyanImagePicker from 'react-native-syan-image-picker';
import ImagePicker from 'react-native-image-picker';
import { PermissionsAndroid } from 'react-native'

class ImagePickerManager {

    static checkPermission = async (permission) => {
        if (__IOS__) {
            return Promise.resolve(true);
        }
        const result = await PermissionsAndroid.request(permission, null)
        // console.log('Permission result:', result);
        return (result === true || result === PermissionsAndroid.RESULTS.GRANTED);
    }

    static showVideoPicker(option = {}) {
        return new Promise((resolve, reject) => {

            SyanImagePicker.openVideoPicker({ ...option}, (error, selectedPhotos) => {
                let data = [];
                if (error) {
                    resolve({ code: StatusCode.FAIL_CODE, data, msg: '取消选择' })
                } else {
                    selectedPhotos.forEach(item => {
                        data.push({
                            size: item.size,
                            width: item.width,
                            height: item.height,
                            path: item.uri,
                            base64: item.base64 ? item.base64.slice(23) : null
                        })
                    });
                    resolve({ code: StatusCode.SUCCESS_CODE, data, msg: '' })
                }
            })
        })
    }

    static showMultipleImagePicker(option = {}) {
        return new Promise((resolve, reject) => {

            SyanImagePicker.showImagePicker({ ...option, quality: 50 }, (error, selectedPhotos) => {
                let data = []
                if (error) {
                    resolve({ code: StatusCode.FAIL_CODE, data, msg: '取消选择' })
                } else {
                    selectedPhotos.forEach(item => {
                        data.push({
                            size: item.size,
                            width: item.width,
                            height: item.height,
                            path: item.uri,
                            base64: item.base64 ? item.base64.slice(23) : null
                        })
                    });
                    resolve({ code: StatusCode.SUCCESS_CODE, data, msg: '' })
                }
            })
        })
    }

    static showImagePicker(option = {}) {
        return new Promise((resolve, reject) => {
            ImagePicker.launchImageLibrary({ ...option, quality: 0.7 }, (response) => {
                let data = null
                if (response.didCancel) {
                    // 取消
                    resolve({ code: StatusCode.FAIL_CODE, data, msg: '取消选择' })
                } else if (response.error) {
                    resolve({ code: StatusCode.FAIL_CODE, data: null, msg: response.error })
                } else if (response.customButton) {
                    resolve({ code: StatusCode.FAIL_CODE, data: null, msg: response.customButton })
                } else {
                    data = {
                        size: response.fileSize,
                        width: response.width,
                        height: response.height,
                        path: __IOS__ ? response.uri.slice(7) : response.path,
                        base64: response.data
                    }
                    resolve({ code: StatusCode.SUCCESS_CODE, data, msg: '' })
                }
            });
        });
    }

    static showLaunchCamera(option = {}) {
        return new Promise(async (resolve, reject) => {
            const checkRes = await this.checkPermission(PermissionsAndroid.PERMISSIONS.CAMERA)
            if (checkRes) {
                ImagePicker.launchCamera({ ...option, quality: 0.7 }, (response) => {
                    // console.log(response)
                    let data = null
                    if (response.didCancel) {
                        // 取消
                        resolve({ code: StatusCode.FAIL_CODE, data, msg: '取消选择' })
                    } else if (response.error) {
                        resolve({ code: StatusCode.FAIL_CODE, data: null, msg: response.error })
                    } else if (response.customButton) {
                        resolve({ code: StatusCode.FAIL_CODE, data: null, msg: response.customButton })
                    } else {
                        data = {
                            size: response.fileSize,
                            width: response.width,
                            height: response.height,
                            path: __IOS__ ? response.uri.slice(7) : response.path,
                            base64: response.data
                        }
                        resolve({ code: StatusCode.SUCCESS_CODE, data, msg: '' })
                    }
                });
            } else {
                resolve({ code: StatusCode.FAIL_CODE, data: null, msg: '没有获取到权限' })
            }
        })
    }
}

export default ImagePickerManager